#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <errno.h>

#define SERVER_PORT 			9527


int main(int argc, char *argv[])
{
	int 		sock;
	int 		status;
	int 		len;
	char		szPkt[128] = {0};
	char 		szBuff[123] = "server message to client.";
	
	
	sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
	struct sockaddr_in ser_addr;
	struct sockaddr_in cli_addr;
	
	memset(&ser_addr,0,sizeof(ser_addr));
	memset(&cli_addr,0,sizeof(cli_addr));
	
	ser_addr.sin_family=AF_INET;
	ser_addr.sin_addr.s_addr=htonl(INADDR_ANY);
	ser_addr.sin_port = htons(SERVER_PORT);
	
	bind(sock,(struct sockaddr*)&ser_addr,sizeof(ser_addr));
	
	//printf("INADDR_ANY=%s\n",INADDR_ANY);
	//printf("ip=%08x\n",htonl(INADDR_ANY));
	
	while(1)
	{
		memset(szPkt,0,sizeof(szPkt));
		recvfrom(sock, szPkt, sizeof(szPkt), 0, (struct sockaddr *)&cli_addr, &len);
		printf("recv result is %s\n",strerror(errno));
		sendto(sock, szBuff, strlen(szBuff), 0, (struct sockaddr*)&cli_addr, sizeof(cli_addr));
		printf("cli_addr.sin_addr.s_addr=%08x\n",cli_addr.sin_addr.s_addr);
		printf("send result is %s\n",strerror(errno));
		
		//status = recv(sock, szPkt, sizeof(szPkt),0);
		printf("szPkt=%s\n",szPkt);
	}
	
	close(sock);

}
